<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="common/head :: htmlhead">
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">草稿箱</div>
                <div class="layui-card-body">
                    <div class="table-reload-btn" style="margin-bottom: 10px;">
                        搜索：
                        <div class="layui-inline">
                            <input class="layui-input" name="title" id="title" placeholder="标题" autocomplete="off">
                        </div>
                        <button class="layui-btn" data-type="reload">搜索</button>
                    </div>

                    <table id="demo" lay-filter="demo" style="margin-left: 15px"></table>
                    <script type="text/html" id="table_operate">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['table', 'element', 'form'], function () {
        var table = layui.table
            , element = layui.element
            , form = layui.form;
        //第一个实例
        table.render({
            elem: '#demo'
            , url: '/getContentlist' //数据接口
            , method: 'post'
            , toolbar: true
            , defaultToolbar: ['filter', 'print']
            , cellMinWidth: 80
            , page: true //开启分页
            , where:{status:"cgx"}
            , cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: 'ID', hide: true, sort: true}
                    , {field: 'title', title: '名称', sort: true}
                    , {field: 'type', title: '所属栏目', sort: true}
                    , {field: 'adduser', title: '操作者', sort: true}
                    , {field: 'addtime', title: '提交时间', sort: true,
                    templet:'<div>{{ layui.util.toDateString(d.addtime, "yyyy-MM-dd") }}</div>'}
                    , {field: 'right', title: '操作', toolbar: '#table_operate', width: 200}
                ]
            ]
        });

        var $ = layui.$, active = {
            reload: function(){
                var cotitle = $('#title');
                //执行重载
                table.reload('demo', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        title: title.val(),
                        status:"cgx"
                    }
                });
            }
        };

        $('.table-reload-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除？', function (index) {
                    $.ajax({
                        url: "/delContent",
                        async: false,
                        type: "POST",
                        dataType: "text",
                        data: {"id": data.id},
                        success: function (aa) {
                            if (aa === "error") {
                                layer.open({
                                    title: '信息'
                                    , content: '删除失败！'
                                    , end: function () {
                                        location.reload();
                                    }
                                });
                            } else if (aa === "success") {
                                layer.open({
                                    title: '信息'
                                    , content: '删除成功！'
                                    , end: function () {
                                        obj.del();
                                        location.reload();
                                    }
                                });
                            }
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                window.location.href = "/draftEdit?id=" + data.id +"&show=no";
            }
        });
    });
</script>
</body>
</html>